/**
 * @Title: LockConditionTest.java
 * @Description:
 * @author zsw
 * <p>
 * Copyright(C) 2018-2019    NPlus
 * Company:   无锡核心信息科技有限公司
 * @version V2.0.0
 * @Date: 17:37 2020/8/14
 * <p>
 * 修改历史:
 * Date                 Author        Version        Description
 * -----------------------------------------------------------------------------------
 * 17:37 2020/8/14       zsw        2.0.0           初始化版本
 */
package com.example.springjpa.util;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockConditionTest {
    public static void main(String[] args) {

        char[] array1 = "1234567".toCharArray();
        char[] array2 = "ABCDEFG".toCharArray();
        Lock lock = new ReentrantLock();
        Condition conditionT1 = lock.newCondition(); //队列1
        Condition conditionT2 = lock.newCondition(); //队列2
        jcdy(array1, lock, conditionT1, conditionT2);
        jcdy(array2, lock, conditionT2, conditionT1);

    }

    private static void jcdy(char[] array1, Lock lock, Condition conditionT1, Condition conditionT2) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    lock.lock();
                    for(char c : array1){
                        System.out.print(c);
                        conditionT2.signal(); //通知第二个队列
                        conditionT1.await(); //阻塞
                    }
                    conditionT2.signal();
                }
                catch (Exception e){}
                finally {
                    lock.unlock();
                }
            }
        }).start();
    }

}
